package IOperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Operation{
    public void work(BookList bookList){
        System.out.println("新增图书");
        int choice = bookList.getUsedSize();

        if(choice == bookList.getBooks().length){
            System.out.println("书架满了，不能放了");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入类型：");
        String type = scanner.nextLine();

        int i = 0;
        for (; i < choice; i++) {
            if(bookList.getBook(i).getName().equals(name)){
                System.out.println("书架已有这本书");
                return;
            }
        }

        bookList.setBook(i,new Book(name,author,price,type));
        bookList.setUsedSize(choice+1);
        System.out.println("新增图书成功");


    }
}
